<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字典名称" prop="dictType">
        <el-select v-model="queryParams.dictType" size="small">
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictType"/>
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="dictLabel">
        <el-input
          v-model="queryParams.dictLabel"
          placeholder="请输入字典标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="数据状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dict:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dict:remove']">
          删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="initData"></right-toolbar>
    </el-row>

    <el-table v-loading="table.loading" :data="table.data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="字典标签" align="center" prop="dictLabel"/>
      <el-table-column label="字典键值" align="center" prop="dictValue"/>
      <el-table-column label="标签颜色" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.listClass">{{ scope.row.listClass && scope.row.listClass !== "" ? scope.row.listClass : "default" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="字典排序" align="center" prop="dictSort"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <status-tag :status-options="statusOptions" :status="scope.row.status"></status-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-link
            type="primary"
            icon="el-icon-edit"
            :underline="false"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']">
            修改
          </el-link>
          <el-link
            type="danger"
            icon="el-icon-delete"
            :underline="false"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dict:remove']">
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="page.total > 0"
      :total="page.total"
      :page.sync="page.current"
      :limit.sync="page.size"
      @pagination="initData"/>

    <value-edit ref="edit" :status-options="statusOptions" :default-dict-type="queryParams.dictType" :list-class-options="listClassOptions" @success="handleQuery"></value-edit>
  </div>
</template>

<script>
import { listData, delData } from "@/api/system/dict/data";
import { listType, getType } from "@/api/system/dict/type";
import ValueEdit from "./components/valueEdit";

export default {
  name: "Data",
  components: {
    ValueEdit
  },
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 默认字典类型
      defaultDictType: "",
      // 状态数据字典
      statusOptions: [],
      // 类型数据字典
      typeOptions: [],
      listClassOptions: [
        {label: 'default', value: ''},
        {label: 'success', value: 'success'},
        {label: 'info', value: 'info'},
        {label: 'warning', value: 'warning'},
        {label: 'danger', value: 'danger'}
      ],
      // 查询参数
      queryParams: {
        dictLabel: undefined,
        dictType: undefined,
        status: undefined
      },
      table: {
        data: [],
        loading: false,
      },
      page: {
        total: 0,
        current: 1,
        size: 10,
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      const dictId = this.$route.params && this.$route.params.dictId;
      this.getType(dictId);
      this.getTypeList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response;
      });
    },
    /** 查询字典类型详细 */
    getType(dictId) {
      getType(dictId).then(response => {
        this.queryParams.dictType = response.dictType;
        this.defaultDictType = response.dictType;
        this.initData();
      });
    },
    /** 查询字典类型列表 */
    getTypeList() {
      listType({
        page: false,
        status: "0"
      }).then(response => {
        this.typeOptions = response;
      });
    },
    /** 查询字典数据列表 */
    initData() {
      this.table.loading = true;
      listData({
        dictLabel: this.queryParams.dictLabel,
        dictType: this.queryParams.dictType,
        status: this.queryParams.status,
        page: true,
        current: this.page.current,
        size: this.page.size
      }).then(response => {
        this.table.data = response.records;
        this.page.total = response.total;
        this.table.loading = false;
      }).catch(() => {
        this.table.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.page.current = 1;
      this.initData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dictType = this.defaultDictType;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.edit.init(null, "新增字典数据");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.edit.init(row.id, "修改字典数据");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.id || this.ids;
      this.$confirm('是否确认删除字典?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delData(dictCodes);
      }).then(() => {
        this.handleQuery();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
